package com.cqcj.login.service;

import com.cqcj.login.mapper.UserMapper;
import com.cqcj.login.pojo.User;
import com.cqcj.login.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class RegisterService {
    //通过mybatis配置文件连接数据库
    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();


    public User selectByUsername(String username){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用mapper在数据库中找有没有账号密码相同的用户
        User user =mapper.selectByUsername(username);
        sqlSession.close();
        return user;
    }
    public boolean register(User user){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.add(user);
        //提交事务
        sqlSession.commit();
        //关闭会话
        sqlSession.close();
        return true;
    }

}
